package com.cloud.bc.service.impl;

import com.cloud.bc.model.entity.BcContacapplys;
import com.cloud.bc.mapper.BcContacapplysMapper;
import com.cloud.bc.model.response.MomenSingleResponse;
import com.cloud.bc.model.response.MyContactResponse;
import com.cloud.bc.service.BcContacapplysService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.bc.service.BcSinglesService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 联系方式申请
 *
 * @author douhaichao code generator
 * @date 2021-01-11 16:56:24
 */
@Service
@AllArgsConstructor
public class BcContacapplysServiceImpl extends ServiceImpl<BcContacapplysMapper, BcContacapplys> implements BcContacapplysService {

    private final BcSinglesService bcSinglesService;

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */
    @Override
    public BcContacapplys selectOne(BcContacapplys req) {
        QueryWrapper queryWrapper = new QueryWrapper<BcContacapplys>();
        queryWrapper.setEntity(req);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 根据条件查询所有
     */
    @Override
    public List<BcContacapplys> selectAll(BcContacapplys req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据对象查询所有分页数据(精确查询)     *      * @return
     */
    @Override
    public IPage<BcContacapplys> selectPage(Page page, BcContacapplys req) {
        IPage<BcContacapplys> list = baseMapper.selectPage(
                page,
                new QueryWrapper<BcContacapplys>().setEntity(req));
        return list;
    }

    /**
     * 根据对象查询所有分页数据(模糊查询)     *      * @return
     */
    @Override
    public IPage<BcContacapplys> selectPageByLike(Page page, BcContacapplys req) {
        QueryWrapper queryWrapper = new QueryWrapper();

        if (null != req.getId()) {
            queryWrapper.eq("id", req.getId());
        }

        if (null != req.getUserId()) {
            queryWrapper.eq("user_id", req.getUserId());
        }

        if (null != req.getTargetId()) {
            queryWrapper.eq("target_id", req.getTargetId());
        }

        if (!StrUtil.isEmpty(req.getPaytype())) {
            queryWrapper.like("paytype", req.getPaytype());
        }

        if (null != req.getCash()) {
            queryWrapper.eq("cash", req.getCash());
        }

        if (null != req.getAgree()) {
            queryWrapper.eq("agree", req.getAgree());
        }

        if (null != req.getDisposeTime()) {
            queryWrapper.eq("dispose_time", req.getDisposeTime());
        }

        if (null != req.getCreateTime()) {
            queryWrapper.eq("create_time", req.getCreateTime());
        }

        if (null != req.getModifyTime()) {
            queryWrapper.eq("modify_time", req.getModifyTime());
        }
        IPage<BcContacapplys> list = baseMapper.selectPage(page, queryWrapper);
        return list;
    }

    /**
     * 根据条件统计数据量     *      * @return
     */
    @Override
    public int count(BcContacapplys req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    public List<MyContactResponse> getContactResponseData(List<BcContacapplys> records, Set<Integer> userIds) {
        List<MomenSingleResponse> momenSingleResponses = bcSinglesService.selectMomenSingle(userIds);
        List<MyContactResponse> list = new ArrayList<>();
        for (BcContacapplys record : records) {
            MyContactResponse myContactResponse = new MyContactResponse();
            //查询单身表
            myContactResponse.setId(record.getId());
            MomenSingleResponse momenSingleResponse = momenSingleResponses.stream().filter(t -> t.getUserId() == record.getUserId()).findFirst().orElse(new MomenSingleResponse());
            myContactResponse.setUserId(record.getUserId());
            Integer age = bcSinglesService.ageConvert(momenSingleResponse.getBirthday());
            myContactResponse.setAge(age);
            myContactResponse.setAgree(record.getAgree());
            myContactResponse.setIncomeCn(momenSingleResponse.getIncomeCn());
            myContactResponse.setIndustryCn(momenSingleResponse.getIndustryCn());
            myContactResponse.setNickname(momenSingleResponse.getNickname());
            myContactResponse.setPhoto(momenSingleResponse.getPhoto());
            myContactResponse.setCreateTime(record.getCreateTime());
            list.add(myContactResponse);
        }
        return list;
    }
}
